package com.noble.glamour.ui.user.check.edit.info;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.LinearLayout;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseActivity;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.ui.user.check.edit.EditKey;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;

import java.util.HashMap;

/**
 * 编辑个人资料页
 * Created by Su on 2017/5/3.
 */
public class UserEditInfoAct extends BaseActivity implements PObserver {
    private UserEditInfoHeadPanel headPanel;
    private UserEditBaseInfoPanel basePanel;
    private UserEditDetailInfoPanel detailPanel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f1_user_edit_info_layout);
        initPanel();
        MsgMgr.getInstance().attach(this);
    }

    private void initPanel() {
        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        headPanel = new UserEditInfoHeadPanel(this);
        basePanel = new UserEditBaseInfoPanel(this);
        detailPanel = new UserEditDetailInfoPanel(this);

//        container.addView(headPanel.getContentView());
        container.addView(basePanel.getContentView());
        container.addView(detailPanel.getContentView());
    }

    @Override
    protected boolean isImmersionBar() {
        return false;
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                headPanel.refreshView();
                basePanel.refreshView();
                detailPanel.refreshView();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    //=================== 内容修改 ===============================
    private boolean isUpdate = false;

    private void setIsUpdate() {
        isUpdate = true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EditKey.EDIT_REQUEST_CODE) {
            HashMap<String, Object> postParams = new HashMap<>();
            switch (resultCode) {
                case EditKey.UPDATE_NICK_NAME:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_nickName, data.getStringExtra("newValue"));
                    break;

                case EditKey.UPDATE_CONNECTION_PHONE:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_mobile, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_mobileAuth, data.getIntExtra("newAuth", 0));
                    break;

                case EditKey.UPDATE_CONNECTION_QQ:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_qq, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_qqAuth, data.getIntExtra("newAuth", 0));
                    break;

                case EditKey.UPDATE_CONNECTION_WECHAT:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_wechat, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_wechatAuth, data.getIntExtra("newAuth", 0));
                    break;

            }
            if (isUpdate) {
                isUpdate = false;
                LoadingDialog.show(this, "正在修改，请稍候...");
                ModuleMgr.getCenterMgr().updateMyInfo(postParams, null);
            }
        }
    }
}
